!function(window,document,$,undefined) {
	var getUrl = getWashUrl(),//地址头部信息
		longitude,//全局经度
		latitude;//全局纬度
	$(document).ready(function() {
		var init = function() {
			bindEvt();
			getLocation();//根据浏览器获取位置信息并给全局变量longitude和latitude赋值
			getNearbyLaundryList();
		};
		var bindEvt = function() {
			$('.map_sign_wp').on('tap',onMapSignTap);
			$('.ld_info_wrap').on('tap','.ld_info',onLdInfoTap);
		}
		var onMapSignTap = function() {
			window.location.href = 'nearlaundrymap.html';
		};
		var onLdInfoTap = function() {
			var laundryId = $(this).attr('id').substr(1);
			// console.log(laundryId);
			window.location.href = 'laundrydetail.html?laundryId='+laundryId+'';
			
		}
		
		function getLocation() {
			if (!navigator.geolocation) {alert('您的浏览器不支持地理定位');return;} //用户浏览器不支持地理定位
			navigator.geolocation.getCurrentPosition(getPosition,handle_error);
			function getPosition(position) {
				var point = {};
				longitude = position.coords.longitude;
				latitude = position.coords.latitude;
				// point = JSON.stringify({longitude:longitude,latitude:latitude});
				localStorage.setItem('userLng',longitude);
				localStorage.setItem('userLat',latitude);
			}
			function handle_error(error) {
				switch(error.code){
				case 1:
					// alert("位置服务被拒绝");
					break;
				case 2:
					alert("获取不到位置信息");
					break;
				case 3:
					alert("获取信息超时");
					break;
				default:
					alert("位置错误");
					break;
				}
			}
		}

		function getNearbyLaundryList() { //店铺列表接口调用
			var url = getUrl+'/common/laundry/getNearbyLaundryList.api',
				signDate = {
					longitude:116.479077,
					latitude:39.916671,
					type:4,
					ssid:getTime(),
					distance:2000000000,//最多不超过30亿米
					pageNumber:1
				};
				$.extend(signDate,{sign:'adsehfiujahfgjuo'});
			$.ajax({
				method:'GET',
				url:url,
				data:signDate,
				dataType:'json',
				success:onLdryListSuccess
			})
		}
		function lightStar(id,n) { //点亮星星的个数方法(传商家id和亮星个数)
			var wrapper = $('#'+id+'').find('.wrapper'),
				stars = wrapper.find('img')
				i,j,len = stars.length-n;
			for(i=0;i<n;i++) {
				stars[i].src = '../images/hl_star.png';
			};
			if(n<stars.length) {
				for(j=n;j<stars.length;j++) {
					stars[j].src = '../images/h_star.png'
				};
			};
		}
		var onLdryListSuccess = function(response) {
			console.log(response);
			response.data[1].laundry.isFavorite ='T';//开发调试修改，最后必须删除！！！！！！！！！！！！！！！
			var data = response.data,//Array(从本地取出购物篮的数量放入此data)
				laundry,//Object
				picture,//Array
				laundryId,
				laundryStar,
				tmpArr = [],
				isFavoriteArr = [],//收藏商家店铺ID
				laundryactivityinfosArr = [],//有优惠活动的商家ID
				businessStatusArr = [],//打烊的商家ID
				// lightStarArr = [],//商家id和点亮的星星的个数
				laundryObj = {},//每一个商家的id和个数的对象
				// washCountArr = localStorage.getItem('washCountArr'),//洗衣篮的数量，做到购物篮时回头来做这里
				ldryPic;
			$.each(data,function(i,obj) {
				laundry = obj.laundry;
				picture = laundry.picture;
				laundryId = laundry.laundryId;
				laundryStar = laundry.star;
				laundryObj['o'+laundryId] = laundryStar;//得到商家Id及其对应亮星数量
				// console.log(laundry);
				// console.log(picture);
				ldryPic =picture && picture[0]||"../images/lypic1.png";
				if(laundry.isFavorite==='T') {isFavoriteArr.push(laundry.laundryId)};
				if(laundry.laundryactivityinfos) {laundryactivityinfosArr.push(laundry.laundryId)};//后续多种优惠标题时该数组中推入对象
				if(laundry.businessStatus==='1'||(!laundry.businessStatus)) {businessStatusArr.push(laundry.laundryId)};
				tmpArr.push(
					'<div data-distance="',obj.distance,'" class="ld_info" id="o',laundry.laundryId,'">',
						'<div class="collect">',
							'<img src="../images/list_icon_collect_fouces.png" alt="">',
						'</div>',
						'<div class="ldy">',
							'<div class="pic">',
								'<img src=',ldryPic,' alt="">',
								'<div class="wash_count">99</div>',
								'<div class="masker_wrap">',
									'<div class="masker"></div>',
									'<div class="close">打烊了</div>',
								'</div>',
							'</div>',
							'<div class="ldy_detail">',
								'<div class="ldy_name">',laundry.name,'</div>',
								'<div class="star_distance">',
									'<div class="star">',
										'<div class="wrapper"> ',
											' <img src="../images/hl_star.png" alt="">',
											' <img src="../images/hl_star.png" alt="">',
											' <img src="../images/hl_star.png" alt="">',
											' <img src="../images/hl_star.png" alt="">',
											' <img src="../images/hl_star.png" alt="">',
										'</div> ',
									'</div>',
								'</div>',
								'<div class="address">',laundry.address,'</div>',
							'</div>',
						'</div>',
						'<div class="distance">',obj.distance,'</div>',
						'<div class="youhui">',
							'<div class="zhe"><img src="../images/yhzk.png" alt=""></div>',
							'<div class="wz">洗衣费用限时优惠</div>',
						'</div>',
						'<div class="bg"></div>',
					'</div>'
				)
			});
			laundryObj['o128'] = 3;//上线前删除！！！！！//上线前删除！！！！！
			laundryObj['o138'] = 2;//上线前删除！！！！！//上线前删除！！！！！
			laundryObj['o141'] = 4;//上线前删除！！！！！//上线前删除！！！！！
			laundryObj['o145'] = 3;//上线前删除！！！！！//上线前删除！！！！！
			// console.log(laundryObj);
			// console.log(isFavoriteArr);
			// console.log(laundryactivityinfosArr);
			// console.log('businessStatusArr:'+businessStatusArr+'');
			$('.ld_info_wrap').html(tmpArr.join(''));
			$.each(isFavoriteArr,function(i,obj) { //显示已收藏
				$('#o'+obj+' .collect').show();
			});
			$.each(laundryactivityinfosArr,function(i,obj) { //商家有优惠活动则显示
				$('#o'+obj+' .youhui').show();
				$('#o'+obj+' .wz').html()
			});
			$.each(businessStatusArr,function(i,obj) { //商家打烊
				$('#o'+obj+' .masker_wrap').css('display','block');//如果用show()方法会给部分元素增加transform(scale(1,1))而不能达到效果//TODO研究hide()
			});
			for(var o in laundryObj) {
				lightStar(o,laundryObj[o]);
			}

		};//onLdryListSuccess


		init();
	})
}(window,document,Zepto)